﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;

namespace RN.ERP.DTO
{
    /// <summary>
    /// 承载统计结构。
    /// </summary>
    public class StatisticsStruct
    {
        private StatisticsHeader[] _header;

        /// <summary>
        /// 获取或设置统计报表表头。
        /// </summary>
        public StatisticsHeader[] Header
        {
            get { return _header; }
            set { _header = value; }
        }

        private StatisticsRow[] _data;

        /// <summary>
        /// 获取或设置统计报表数据。
        /// </summary>
        public StatisticsRow[] Data
        {
            get { return _data; }
            set { _data = value; }
        }

        private StatisticsCell[] _rowTitle;

        /// <summary>
        /// 获取或设置统计报表行标题。
        /// </summary>
        public StatisticsCell[] RowTitle
        {
            get { return _rowTitle; }
            set { _rowTitle = value; }
        }

        public class StatisticsRow
        {
            private StatisticsCell[] _cells;

            /// <summary>
            /// 获取或设置单元数据。
            /// </summary>
            public StatisticsCell[] DataCells
            {
                get { return _cells; }
                set { _cells = value; }
            }

            public StatisticsCell this[int index]
            {
                get
                {
                    if (_cells != null)
                    {
                        return _cells[index];
                    }
                    else
                    {
                        return null;
                    }
                }
            }
        }

        public class StatisticsHeader
        {
            public StatisticsHeader() { }
            
            public StatisticsHeader(params string[] columnNames)
            {
                List<StatisticsStruct.StatisticsCell> hcell_list = new List<StatisticsStruct.StatisticsCell>();
                foreach (var name in columnNames)
                {
                    StatisticsStruct.StatisticsCell hcell = new StatisticsStruct.StatisticsCell { Value = name };
                    hcell_list.Add(hcell);
                }
                this.HeaderCells = hcell_list.ToArray();
            }

            private StatisticsCell[] _cells;

            /// <summary>
            /// 获取或设置单元表头。
            /// </summary>
            public StatisticsCell[] HeaderCells
            {
                get { return _cells; }
                set { _cells = value; }
            }

            public StatisticsCell this[int index]
            {
                get
                {
                    if (_cells != null)
                    {
                        return _cells[index];
                    }
                    else
                    {
                        return null;
                    }
                }
            }
        }

        public class StatisticsCell
        {
            public string LinkParams { get; set; }
            public string Value { get; set; }
            public int ColSpan { get; set; }
            public int RowSpan { get; set; }
        }

        #region 方法

        public DataTable ToDataTable()
        {
            DataTable result = new DataTable();
            if (this._header.Count() == 0)
            {
                return result;
            }
            foreach (StatisticsCell headerCell in this._header.LastOrDefault().HeaderCells)
            {
                result.Columns.Add(new DataColumn()
                {
                    ColumnName = headerCell.Value
                });
            }
            foreach (StatisticsRow row in this._data)
            {
                DataRow dr = result.NewRow();
                int index = 0;
                foreach (StatisticsCell cell in row.DataCells)
                {
                    dr[index] = cell.Value;
                    index++;
                }
                result.Rows.Add(dr);
            }
            return result;
        }

        #endregion
    }

}
